package com.bkitmobile.kesu.appengine.storingdata.server.entity;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import com.bkitmobile.kesu.appengine.storingdata.server.MD5;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
@PersistenceCapable
public class User {
	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
	
	public static final int BLOCK_PREMIUM_PLACE = 10;
	
	@Persistent
	private String id;
	
	@Persistent
	private String password;
	
	@Persistent
	private String firstName;

	@Persistent
	private String lastName;

	@Persistent
	private String birthDay;
	
	@Persistent
	private String email;
	
	@Persistent
	private String address;
	
	@Persistent
	private String phoneNumber;
	
	@Persistent
	private String job;
	
	@Persistent
	private String status;
	
	@Persistent
	private double currentLongitude;
	
	@Persistent
	private double currentLatitude;
	
	@Persistent
	private boolean isBlocked;
	
	@Persistent
	private boolean sex;
	
	@Persistent
	private boolean isAvailable;
	
	@Persistent
	private String identityCardNumber;
	
	@Persistent
	private boolean loggedin;
	
	@Persistent
	private Date dateReg;
	
	@Persistent
	private Date lastUpdated;
	
	// n to n relation with friends
	@Persistent
    private Set<Key> friendsList;
	
	//n to n relation with view invisible
	
	//can view user invisible
	@Persistent
    private Set<Key> viewInvisiblePermision;
	
	@Persistent
	private Set<Key> waitingAccept;
	
	@Persistent
	private Set<Key> isWaitingAccept;
	
	//when invisible, users view me
	@Persistent
	private Set<Key> userViewInvisible;
	
	@Persistent
	private Set<Key> manageUser;
	
	@Persistent
    private Set<Key> inviteCmt;
	
	@Persistent
    private String pid;
	
	@Persistent
	private Set<Key> messages;
	
	@Persistent
	private Set<Key> placeCreated;
	
	@Persistent
	private Set<Key> favoritePlace;
	

	//private LinkedHashMap<Key,String> inviteComment;
	@Persistent
	private int numberPlace;
	
	@Persistent
	private Set<Key> myPlace;
	
	@Persistent
	private int placeLeft;
	
	
	@Persistent
    private Blob avatar;
	public User(String userName,String password,String firstName,
		String lastName, String identityCardNumber, String birthday, String email,String address, String phoneNumber,
		String job, boolean sex, String status,float currentLongitude,float currentLatitude,boolean isBlocked,
		boolean isAvailable,Date dateReg
		)
	{
		key = KeyFactory.createKey(this.getClass().getSimpleName(), userName);
		id = userName;
		try {
			password = MD5.convert(password);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setPassword(password);
		this.setFirstName(firstName);
		this.setLastName(lastName);
		this.setBirthDay(birthday);
		this.setIdentityCardNumber(identityCardNumber);
		this.setEmail(email);
		this.setAddress(address);
		this.setPhoneNumber(phoneNumber);
		this.setJob(job);
		this.setSex(sex);
		this.setStatus(status);
		this.setCurrentLatitude(currentLatitude);
		this.setCurrentLongitude(currentLongitude);
		this.setBlocked(isBlocked);
		this.setAvailable(isAvailable);
		loggedin = false;
		this.dateReg = dateReg;
		lastUpdated = dateReg;
		this.isAvailable = false;
		this.isBlocked = false;
		friendsList = new LinkedHashSet<Key>();
		waitingAccept = new LinkedHashSet<Key>();
		isWaitingAccept = new LinkedHashSet<Key>();
		setInviteCmt(new LinkedHashSet<Key>());
		placeLeft = 0;
	}
		
	public void setKey(Key key) {
		this.key = key;
	}

	public Key getKey() {
		return key;
	}
	public void addPlaceCount(int blocks)
	{
		placeLeft += blocks*BLOCK_PREMIUM_PLACE;
	}
	public void addPlacePremium()
	{
		placeLeft--;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword() {
		return password;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setBirthDay(String birthDay) {
		this.birthDay = birthDay;
	}

	public String getBirthDay() {
		return birthDay;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getEmail() {
		return email;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getAddress() {
		return address;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setJob(String job) {
		this.job = job;
	}

	public String getJob() {
		return job;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getStatus() {
		return status;
	}



	public void setBlocked(boolean isBlocked) {
		this.isBlocked = isBlocked;
	}

	public boolean isBlocked() {
		return isBlocked;
	}

	public void setSex(boolean sex) {
		this.sex = sex;
	}

	public boolean getSex() {
		return sex;
	}

	public void setAvailable(boolean isAvailable) {
		this.isAvailable = isAvailable;
	}

	public boolean isAvailable() {
		return isAvailable;
	}

	public void setIdentityCardNumber(String identityCardNumber) {
		this.identityCardNumber = identityCardNumber;
	}

	public String getIdentityCardNumber() {
		return identityCardNumber;
	}

	public void setCurrentLongitude(double currentLongitude) {
		this.currentLongitude = currentLongitude;
	}

	public double getCurrentLongitude() {
		return currentLongitude;
	}

	public void setCurrentLatitude(double currentLatitude) {
		this.currentLatitude = currentLatitude;
	}

	public double getCurrentLatitude() {
		return currentLatitude;
	}

	public void setFriendsList(Set<Key> friendsList) {
		this.friendsList = friendsList;
	}

	public Set<Key> getFriendsList() {
		return friendsList;
	}

	public void setViewInvisiblePermision(Set<Key> viewInvisiblePermision) {
		this.viewInvisiblePermision = viewInvisiblePermision;
	}

	public Set<Key> getViewInvisiblePermision() {
		return viewInvisiblePermision;
	}

	public void setUserViewInvisible(Set<Key> userViewInvisible) {
		this.userViewInvisible = userViewInvisible;
	}

	public Set<Key> getUserViewInvisible() {
		return userViewInvisible;
	}

	public void setLoggedin(boolean loggedin) {
		this.loggedin = loggedin;
	}

	public boolean isLoggedin() {
		return loggedin;
	}

	public void setDateReg(Date dateReg) {
		this.dateReg = dateReg;
	}

	public Date getDateReg() {
		return dateReg;
	}

	public void setLastUpdated(Date lastUpdated) {
		this.lastUpdated = lastUpdated;
	}

	public Date getLastUpdated() {
		return lastUpdated;
	}

	public void setManageUser(Set<Key> manageUser) {
		this.manageUser = manageUser;
	}

	public Set<Key> getManageUser() {
		return manageUser;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}

	public void setWaitingAccept(Set<Key> waitingAccept) {
		this.waitingAccept = waitingAccept;
	}

	public Set<Key> getWaitingAccept() {
		return waitingAccept;
	}

	public void setIsWaitingAccept(Set<Key> isWaitingAccept) {
		this.isWaitingAccept = isWaitingAccept;
	}

	public Set<Key> getIsWaitingAccept() {
		return isWaitingAccept;
	}

	public void setInviteCmt(Set<Key> inviteCmt) {
		this.inviteCmt = inviteCmt;
	}

	public Set<Key> getInviteCmt() {
		return inviteCmt;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getPid() {
		return pid;
	}

	public void setMessages(Set<Key> messages) {
		this.messages = messages;
	}

	public Set<Key> getMessages() {
		return messages;
	}

	public void setPlacesCreated(Set<Key> locationsCreated) {
		this.placeCreated = locationsCreated;
	}

	public Set<Key> getPlacesCreated() {
		return placeCreated;
	}

	public void setFavoritePlace(Set<Key> favoritePlace) {
		this.favoritePlace = favoritePlace;
	}

	public Set<Key> getFavoritePlace() {
		return favoritePlace;
	}

	public void setNumberPlace(int numberPlace) {
		this.numberPlace = numberPlace;
	}

	public int getNumberPlace() {
		return numberPlace;
	}

	public void setMyPlace(Set<Key> myPlace) {
		this.myPlace = myPlace;
	}

	public Set<Key> getMyPlace() {
		return myPlace;
	}

	public void setPlaceLeft(int placeLeft) {
		this.placeLeft = placeLeft;
	}

	public int getPlaceLeft() {
		return placeLeft;
	}

	public void setAvatar(Blob avatar) {
		this.avatar = avatar;
	}

	public Blob getAvatar() {
		return avatar;
	}
}
